<template>
    <van-nav-bar
        title="新增收货地址"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
    />

    <van-address-edit
        :area-list="areaList"
        show-set-default
        :address-info="address"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
    />

</template>

<script>
import { areaList } from '@vant/area-data'
export default {
    data() {
        return {
            areaList,
            LoginUser:this.$cookies.get('LoginUser')
        }
    },
    methods: {
        onClickLeft()
        {
            this.$router.go(-1)
        },
        async onSave(item)
        {
            let data = {
                consignee:item.name,
                mobile:item.tel,
                code:item.areaCode,
                address:item.addressDetail,
                status:item.isDefault ? 1 : 0,
                userid:this.LoginUser.id
            }
            // 添加赋值给api
            let result = await this.$api.AddressAdd(data)

            if(result.code === 1)
            {
                this.$notify({
                    type: "success",
                    message: result.msg,
                    duration:1500,
                    onClose:() => {
                       
                        this.$router.push(result.url)
                    }
                });
                return false
            }else{
                this.$notify({
                    type: "danger",
                    message: result.msg,
                    duration:1500
                });
                return false
            }
        }
    },
    computed:{
        address()
        {
            let data ={
                name:this.LoginUser.nickname
            }

            let area = this.LoginUser.region_text.split('-')

            // console.log(area)

            if(!Array.isArray(area))
            {
                return false
            }

            if(area[0])
            {
                data.province = area[0]
                data.areaCode = this.LoginUser.province.toString()
            }

            if(area[1])
            {
                data.city = area[1]
                data.areaCode = this.LoginUser.city.toString()
            }

            if(area[2])
            {
                data.county = area[2]
                data.areaCode = this.LoginUser.district.toString()
            }

            return data
        }
    }
}
</script>